package com.base.service.system.utils;

import java.util.ArrayList;
import java.util.List;

import com.base.service.system.vo.AuthVO;

/**
 * @author start
 */
public class AuthListUtils {
	
	public static List<AuthVO> tree(Long parentId,List<AuthVO> allAuths) {
		List<AuthVO> parentAuths=allAuths.stream().filter(t->t.getParentId().equals(parentId)).toList();
		child(parentAuths,allAuths);
		return parentAuths;
	}
	
	public static void child(List<AuthVO> parentAuths,List<AuthVO> allAuths) {
		for(AuthVO a:parentAuths) {
			a.setItems(allAuths.stream().filter(t->t.getParentId().equals(a.getId())).toList());
			child(a.getItems(),allAuths);
		}
	}
	
	public static List<AuthVO> build(List<AuthVO> auths,List<Long> authIds) {
		List<AuthVO> items=new ArrayList<>();
		for(AuthVO v:auths) {
			v.setItems(build(v.getItems(),authIds));
			if(authIds.contains(v.getId())) {
				items.add(v);
			}else if(v.getItems().size()>0) {
				//子级有权限父级未设权限也显示
				items.add(v);
			}
		}
		return items;
	}
	
}
